import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/accident_info_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';

///事故事件
class AccidentEventsController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索字符串
  String? search;

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 选中的事故来源
  String? accidentSource;

  /// 选中的事故等级
  String? accidentLevel;

  /// 选中的事故类型
  String? accidentType;

  /// 选中的处置状态
  String? disposalStatus;

  /// 选中的上报状态
  String? reportingStatus;

  @override
  void onInit() {
    super.onInit();
    init();
  }

  /// 初始化筛选条件
  Future<void> init() async {
    // 获取事故来源字典
    final List<GetDictAllEntity> sourceDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'sgly_1OKY5e03');
    // 获取事故等级字典
    final List<GetDictAllEntity> levelDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'sgdj_l5ETCuTE');
    // 获取事故类型字典
    final List<GetDictAllEntity> typeDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'sglx_Nu5eHlv1');
    // 获取处置状态字典
    final List<GetDictAllEntity> disposalDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'sgczzt_AoX63C87');
    // 获取上报状态字典
    final List<GetDictAllEntity> reportingDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'sgsbzt_KlzpfCFu');

    // 来源
    final List<FilterEntitiesBean> sourceType = [];
    for (var item in sourceDictList) {
      sourceType.add(FilterEntitiesBean(name: item.getShowName(), value: item.getId()));
    }

    // 等级
    final List<FilterEntitiesBean> levelType = [];
    for (var item in levelDictList) {
      levelType.add(FilterEntitiesBean(name: item.getShowName(), value: item.getId()));
    }

    // 类型
    final List<FilterEntitiesBean> typeType = [];
    for (var item in typeDictList) {
      typeType.add(FilterEntitiesBean(name: item.getShowName(), value: item.getId()));
    }

    // 处置状态
    final List<FilterEntitiesBean> disposalType = [];
    for (var item in disposalDictList) {
      disposalType.add(FilterEntitiesBean(name: item.getShowName(), value: item.getId()));
    }

    // 上报状态
    final List<FilterEntitiesBean> reportingType = [];
    for (var item in reportingDictList) {
      reportingType.add(FilterEntitiesBean(name: item.getShowName(), value: item.getId()));
    }

    //筛选控制bean
    final FilterBean filterBean = FilterBean(title: '事故来源', filterList: sourceType);
    final FilterBean filterBean1 = FilterBean(title: '事故类型', filterList: typeType);
    final FilterBean filterBean2 = FilterBean(title: '事故等级', filterList: levelType);
    final FilterBean filterBean3 = FilterBean(title: '处置状态', filterList: disposalType);
    final FilterBean filterBean4 = FilterBean(title: '上报状态', filterList: reportingType);

    filterList.add(filterBean);
    filterList.add(filterBean1);
    filterList.add(filterBean2);
    filterList.add(filterBean3);
    filterList.add(filterBean4);
  }

  // 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    accidentSource = null;
    accidentLevel = null;
    accidentType = null;
    disposalStatus = null;
    reportingStatus = null;
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '事故来源') {
        accidentSource = i.chosenValue[0].value as String;
      }
      if (i.title == '事故等级') {
        accidentLevel = i.chosenValue[0].value as String;
      }
      if (i.title == '事故类型') {
        accidentType = i.chosenValue[0].value as String;
      }
      if (i.title == '处置状态') {
        disposalStatus = i.chosenValue[0].value as String;
      }
      if (i.title == '上报状态') {
        reportingStatus = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  //筛选按钮
  void onFilterButtonPressed() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  // 获取数据
  Future<List<AccidentInfoEntity>> getListData(int pageNum, int pageSize) async {
    // 获取列表接口
    final res = await HttpUtil().generateGetRequest<List<AccidentInfoEntity>>(
      Interface.getAccidentInfoPage,
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'accidentName': search,
        'accidentSource': accidentSource,
        'accidentLevel': accidentLevel,
        'accidentType': accidentType,
        'disposalStatus': disposalStatus,
        'reportingStatus': reportingStatus,
      },
    ).execute();
    List<AccidentInfoEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }
}
